package com.liyi.controller;

import javax.inject.Inject;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.liyi.dto.ComStatementDto;
import com.liyi.dto.DepStatementDto;
import com.liyi.dto.EmpStatementDto;
import com.liyi.service.StatementService;
import com.zero.basic.exception.SmsErrorConstant;
import com.zero.basic.exception.SmsRuntimeException;
import com.zero.basic.exception.SmsStatusCode;
import com.zero.basic.util.JsonCreator;

@Controller("salaryStatementsControllerLiyi")
@RequestMapping("salarystatements")
public class StatementsController {
	
	@Inject
	private StatementService statementServiceLiyi;
	
	/* 获取 总公司 在某 [年]度 某 [月]度 的统计报表 */
	@RequestMapping("/com")
	@ResponseBody
	public Object getComStatement(Integer year, Integer month, HttpServletResponse response, HttpSession session) {
		// 声明返回的JSON对象
		ObjectNode objectNode = JsonCreator.generateJsonObj();
		try {
			ComStatementDto statement = statementServiceLiyi.getComStatement(year, month);
			ComStatementDto[] re = new ComStatementDto[2];
			re[0] = new ComStatementDto();
			re[0].setYear("年度");
			re[0].setMonth("月度");
			re[0].setTotalSalary("总工资");
			re[0].setMaxSalary("最高工资");
			re[0].setMinSalary("最低工资");
			re[0].setAvgSalary("平均工资");
			re[1] = statement;
			// 封装所有员工的工资项和服务器响应信息
			objectNode.putPOJO("response", re);
			objectNode.put("message", "获取公司年度月度报表成功");
			objectNode.put("statusCode", SmsStatusCode.SUC_CODE);

			// System.out.println(objectNode);
		} catch (SmsRuntimeException e) {
			objectNode.put("message", e.getErrorDescription());
			objectNode.put("error", e.getError());
			objectNode.put("statusCode", e.getStatusCode());
		} catch (Exception e) {
			e.printStackTrace();
			objectNode.put("message", SmsErrorConstant.SERVER_ERROR_MSG);
			objectNode.put("error", SmsErrorConstant.WEB_APPLICATION);
			objectNode.put("statusCode", SmsStatusCode.SERVER_ERROR);
		}
		// 返回JSON对象
		return objectNode;
	}

	/* 计算某 [部门] 下所有员工在某 [月] 的所有 [计算工资项] 的值并写入数据库 */
	@RequestMapping("/dep")
	@ResponseBody
	public Object getDepStatement(Integer year, Integer month, Integer depId, HttpServletResponse response, HttpSession session) {
		// 声明返回的JSON对象
		ObjectNode objectNode = JsonCreator.generateJsonObj();
		try {
			DepStatementDto statement = statementServiceLiyi.getDepStatement(year, month, depId);
			DepStatementDto[] re = new DepStatementDto[2];
			re[0] = new DepStatementDto();
			re[0].setDepId("部门编号");
			re[0].setDepName("部门名称");
			re[0].setYear("年度");
			re[0].setMonth("月度");
			re[0].setTotalBaseSalary("总基本工资");
			re[0].setMaxBaseSalary("最高基本工资");
			re[0].setMinBaseSalary("最低基本工资");
			re[0].setAvgBaseSalary("平均基本工资");
			re[1] = statement;
			// 封装空返回值和服务器响应信息
			objectNode.putPOJO("response", re);
			objectNode.put("message", "获取部门年度月度报表成功");
			objectNode.put("statusCode", SmsStatusCode.SUC_CODE);

			// System.out.println(objectNode);
		} catch (SmsRuntimeException e) {
			objectNode.put("message", e.getErrorDescription());
			objectNode.put("error", e.getError());
			objectNode.put("statusCode", e.getStatusCode());
		} catch (Exception e) {
			e.printStackTrace();
			objectNode.put("message", SmsErrorConstant.SERVER_ERROR_MSG);
			objectNode.put("error", SmsErrorConstant.WEB_APPLICATION);
			objectNode.put("statusCode", SmsStatusCode.SERVER_ERROR);
		}
		// 返回JSON对象
		return objectNode;
	}
	
	/* 设置某 [员工] 的 在某 [月] 的 [工资项] 状态为 发放 */
	@RequestMapping("/emp")
	@ResponseBody
	public Object getEmpStatement(Integer year, Integer empId, HttpServletResponse response, HttpSession session) {
		// 声明返回的JSON对象
		ObjectNode objectNode = JsonCreator.generateJsonObj();
		try {
			EmpStatementDto statement = statementServiceLiyi.getEmpStatement(year, empId);
			EmpStatementDto[] re = new EmpStatementDto[2];
			re[0] = new EmpStatementDto();
			re[0].setEmpId("员工编号");
			re[0].setEmpName("员工姓名");
			re[0].setYear("年度");
			re[0].setTotalBaseSalary("总基本工资");
			re[0].setTotalHousingFund("总公积金");
			re[0].setTotalMedical("总医疗保险");
			re[0].setTotalOverworkSalary("总加班工资");
			re[0].setTotalPension("总养老保险");
			re[0].setTotalSickleaveHold("总病假扣款");
			re[1] = statement;
			// 封装空返回值和服务器响应信息
			objectNode.putPOJO("response", re);
			objectNode.put("message", "获取员工年度报表成功");
			objectNode.put("statusCode", SmsStatusCode.SUC_CODE);

			// System.out.println(objectNode);
		} catch (SmsRuntimeException e) {
			objectNode.put("message", e.getErrorDescription());
			objectNode.put("error", e.getError());
			objectNode.put("statusCode", e.getStatusCode());
		} catch (Exception e) {
			e.printStackTrace();
			objectNode.put("message", SmsErrorConstant.SERVER_ERROR_MSG);
			objectNode.put("error", SmsErrorConstant.WEB_APPLICATION);
			objectNode.put("statusCode", SmsStatusCode.SERVER_ERROR);
		}
		// 返回JSON对象
		return objectNode;
	}
}